package org.openapitools.common;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author lemon
 */
@Data
@Schema(name = "RS", description = "返回结果")
public class Result<T> {
    @Schema(description = "说明")
    private String message = "";
    @Schema(description = "是否成功")
    private boolean success = true;
    @Schema(description = "数据")
    private T data;
    @Schema(description = "异常")
    private String e = "";
    @Schema(description = "返回码")
    private String code = ResultCodeEnum.SUCCESS.getCode();

    /**
     * 时间戳，表示响应生成的时间。
     */
    private long timestamp = System.currentTimeMillis();

    public long getTimestamp() {
        return System.currentTimeMillis();
    }
    public void setTimestamp() {
        this.timestamp = System.currentTimeMillis();
    }

    private static final Logger logger = LoggerFactory.getLogger(Result.class);

    public static <T> Result<T> success(T returnObj) {
        Result<T> result = new Result<>();
        result.setSuccess(true);
        result.setData(returnObj);
        result.setMessage("操作成功");
        return result;
    }

    public static <T> Result<T> fs_success(T returnObj, String message) {
        Result<T> result = new Result<>();
        result.setCode("000000");
        result.setSuccess(true);
        result.setData(returnObj);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> fs_success(T returnObj) {
        Result<T> result = new Result<>();
        result.setCode("000000");
        result.setSuccess(true);
        result.setData(returnObj);
        result.setMessage("处理数据成功");
        return result;
    }

    public static <T> Result<T> fs_error(String code, T data, String message) {
        logger.error(String.valueOf(message));
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setSuccess(false);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> fs_error(T data, String message) {
        logger.error(String.valueOf(message));
        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.SERVER_ERROR.getCode());
        result.setSuccess(false);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> fs_error(T msg) {
        logger.error(String.valueOf(msg));
        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.SERVER_ERROR.getCode());
        result.setSuccess(false);
        result.setMessage("数据同步失败");
        result.setData(msg);
        return result;
    }
    public static <T> Result<T> error(String msg) {
        logger.error(String.valueOf(msg));
        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.SERVER_ERROR.getCode());
        result.setSuccess(false);
        result.setData((T) msg);
        result.setMessage(msg);
        return result;
    }

    public static <T> Result<T> error(T data, String msg) {
        logger.error(String.valueOf(msg));
        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.SERVER_ERROR.getCode());
        result.setSuccess(false);
        result.setData(data);
        result.setMessage(msg);
        return result;
    }

    public static <T> Result<T> failure(String msg) {
        logger.error(String.valueOf(msg));
        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.SERVER_ERROR.getCode());
        result.setSuccess(false);
        result.setMessage(msg);
        return result;
    }

    public static <T> Result<T> failure(String msg, String... formatter) {
        logger.error(msg, formatter);
        Result<T> result = new Result<>();
        result.setSuccess(false);
        result.setCode(ResultCodeEnum.SERVER_ERROR.getCode());
        result.setMessage(msg);
        return result;
    }

    public static <T> Result<T> error(String msg, String... formatter) {
        logger.error(msg, formatter);
        Result<T> result = new Result<>();
        result.setSuccess(false);
        result.setCode(ResultCodeEnum.SERVER_ERROR.getCode());
        result.setMessage(msg);
        return result;
    }

    public static <T> Result<T> fs_error(String msg, String... formatter) {
        logger.error(msg, formatter);
        Result<T> result = new Result<>();
        result.setSuccess(false);
        result.setCode(ResultCodeEnum.SERVER_ERROR.getCode());
        result.setMessage(msg);
        return result;
    }

    public static <T> Result<T> exception(Exception e, String msg) {
        logger.error(e.getMessage(), e);
        Result<T> result = new Result<>();
        result.setSuccess(false);
        result.setMessage(msg);
        result.setCode(ResultCodeEnum.SERVER_ERROR.getCode());
        result.setE(e.getMessage());
        return result;
    }

    @Override
    public String toString() {
        return "RS{" +
                "success=" + success +
                ", code=" + code +
                ", data=" + data +
                ", message='" + message + '\'' +
                '}';
    }

}
